﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using NHibernate;

namespace Advisor.Entities
{
    public class MembersProjectDAO
    {
        private Users m_User;
        public MembersProjectDAO(Users user)
        {
            this.m_User = user;
        }
         /// <summary>
        /// Retorna todas os registros da tabela MembersProject. 
        /// </summary>
        /// <param name="column"></param>
        /// <param name="maximumRows"></param>
        /// <param name="startRowIndex"></param>
        /// <returns></returns>
        internal IList GetMembersProjects(string column, int maximumRows, int startRowIndex)
        {
            ISession session = NHibernateSession.GetSession();
            string sql = "FROM MEMBERSPROJECTMAPPINGS as membersprojects";

            //Ordenação
            if (!String.IsNullOrEmpty(column))
            {
                sql += " ORDER BY membersprojects." + column;
            }
            IQuery query = session.CreateQuery(sql);

            //Paginando
            query.SetFirstResult(startRowIndex);
            query.SetMaxResults(maximumRows);

            return query.List();
        }
        /// <summary>
        ///  Método para inserir um MemberProject no banco. 
        /// </summary>
        /// <param name="history"></param>
        internal void InsertMemberProject(Membersprojectmappings memberProject)
        {
            ISession session = NHibernateSession.GetSession();
            session.Save(memberProject);
            session.Close();
        }

        /// <summary>
        /// Retorna uma instância da tabela MembersProject passando o id;
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        internal Membersprojectmappings GetMemberProject(int id)
        {
            ISession session = NHibernateSession.GetSession();
            return (Membersprojectmappings)session.Get(typeof(Membersprojectmappings), id);
        }

        /// <summary>
        /// Remove um MemberProject do banco
        /// </summary>
        /// <param name="history"></param>

        internal void DeleteMembersProject(Membersprojectmappings memberProject)

        {
            ISession sessions = NHibernateSession.GetSession();
            sessions.Delete(memberProject);
            sessions.Close();
        }

        /// <summary>
        /// Atualiza um MemberProject do banco. 
        /// </summary>
        /// <param name="history"></param>

        internal void UpdateMembersProject(Membersprojectmappings memberProject)

        {
            ISession session = NHibernateSession.GetSession();
            session.Update(memberProject);
            //Comando que ativa as alterações no banco de dados. 
            session.Flush();
            session.Close();
        }

    }
    }

